/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biermanager.componentes;

import biermanager.nucleo.ConstantesDeCaminho;
import biermanager.nucleo.ConstantesDeJogo;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.ColorSpaceType;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import org.jdesktop.swingx.painter.AbstractAreaPainter.Style;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.HorizontalAlignment;
import org.jdesktop.swingx.painter.AbstractLayoutPainter.VerticalAlignment;
import org.jdesktop.swingx.painter.AbstractPainter.Interpolation;
import org.jdesktop.swingx.painter.CapsulePainter;
import org.jdesktop.swingx.painter.CapsulePainter.Portion;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.GlossPainter;
import org.jdesktop.swingx.painter.ImagePainter;
import org.jdesktop.swingx.painter.MattePainter;
import org.jdesktop.swingx.painter.Painter;
import org.jdesktop.swingx.painter.PinstripePainter;
import org.jdesktop.swingx.painter.ShapePainter;
import org.jdesktop.swingx.painter.TextPainter;
import org.jdesktop.swingx.painter.effects.AreaEffect;

/**
 *
 * @author Tiago Silva Loureiro
 */
public class PintorDeBackground {

    /**
     * 
     * @param j
     * @param caminhoDaImagemDeFundo
     * @return
     */
    public Painter pintarPainelTopo(JComponent j, String caminhoDaImagemDeFundo) {
        Color cor1 = new Color(200, 0, 0,0);
        Color cor2 = new Color(200, 0, 0,255);
        Point2D start = new Point2D.Float(0, 0);
        Point2D end = new Point2D.Float(0, (float) (j.getPreferredSize().getHeight() - 1));
        float[] dist = {0.0f, 1.0f};
        Color[] colors = {cor1, cor2};
        LinearGradientPaint gradientePaint = new LinearGradientPaint(start, end, dist, colors);
        MattePainter pintorSolido = new MattePainter(gradientePaint);

        Color cor3 = new Color(150, 0, 0,0);
        Color cor4 = new Color(150, 0, 0,255);
        Point2D start2 = new Point2D.Float(0, 0);
        Point2D end2 = new Point2D.Float((float) (j.getWidth() - 1),0);
        float[] dist2 = {0.0f, 1.0f};
        Color[] colors2 = {cor3, cor4};
        LinearGradientPaint gradientePaint2 = new LinearGradientPaint(start2, end2, dist2, colors2);
        MattePainter pintorSolido2 = new MattePainter(gradientePaint2);


/*        BufferedImage imagemDeFundo = null;
        try {
            imagemDeFundo = ImageIO.read(getClass().getResource(caminhoDaImagemDeFundo));
        } catch (IOException ex) {
            Logger.getLogger(PintorDeBackground.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        ImagePainter pintorImagem = new ImagePainter(imagemDeFundo);
        pintorImagem.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        pintorImagem.setAntialiasing(true);

        BufferedImage sombraImagemFundo = new BufferedImage(imagemDeFundo.getWidth() + 4, imagemDeFundo.getHeight() + 4, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graficoSombra = sombraImagemFundo.createGraphics();
        graficoSombra.drawImage(imagemDeFundo, null, 2, 2);
        graficoSombra.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, .3f));
        graficoSombra.setColor(Color.lightGray);
        graficoSombra.fillRect(0, 0, imagemDeFundo.getWidth(), imagemDeFundo.getHeight());
        graficoSombra.dispose();
        for (int x = 0; x < imagemDeFundo.getWidth(); x++) {
            for (int y = 0; y < imagemDeFundo.getHeight(); y++) {
                int rgb = imagemDeFundo.getRGB(x, y);
                rgb = (int) ((rgb >> 24 & 0xFF) * .3f) << 24 |
                        Color.lightGray.getRGB() & 0x00FFFFFF;
                sombraImagemFundo.setRGB(x, y, rgb);
            }
        }
        ImagePainter pintorSombraImagem = new ImagePainter(sombraImagemFundo);
        pintorSombraImagem.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        pintorSombraImagem.setAntialiasing(true);*/

        ImagePainter pintorImagem = (ImagePainter) this.pintorIcone(caminhoDaImagemDeFundo, 50, 50);
        pintorImagem.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        pintorImagem.setAntialiasing(true);
        int larguraDoComponente = (int) j.getPreferredSize().getWidth();
        String titulo = ConstantesDeJogo.tituloDoJogo;
        Insets insetsImagem = pintorImagem.getInsets();

        Font fonteTrueType = null;
        try {
            fonteTrueType = Font.createFont(Font.TRUETYPE_FONT, getClass().getClassLoader().getResourceAsStream("biermanager/fontes/rabiscos.ttf"));
        } catch (FontFormatException ex) {
            Logger.getLogger(PainelTopo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PainelTopo.class.getName()).log(Level.SEVERE, null, ex);
        }
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fonteTrueType);

        TextPainter pintorDeTexto = new TextPainter(titulo, new Font(fonteTrueType.getFontName(), Font.PLAIN, 60));
        //pintorDeTexto.setInsets(new Insets(insetsImagem.top, insetsImagem.left, insetsImagem.bottom, larguraDoComponente - .getWidth()));
        pintorDeTexto.setFillPaint(new Color(255, 255, 255,225));
        pintorDeTexto.setHorizontalAlignment(HorizontalAlignment.CENTER);
        pintorDeTexto.setVerticalAlignment(VerticalAlignment.CENTER);


        TextPainter pintorDeSombraTexto = new TextPainter(titulo, new Font(fonteTrueType.getFontName(), Font.PLAIN, 60));
        pintorDeSombraTexto.setFillPaint(new Color(50, 50, 50, 128));
        pintorDeSombraTexto.setHorizontalAlignment(HorizontalAlignment.CENTER);
        pintorDeSombraTexto.setVerticalAlignment(VerticalAlignment.CENTER);
        insetsImagem = pintorDeTexto.getInsets();
        pintorDeSombraTexto.setInsets(new Insets(insetsImagem.top + 9, insetsImagem.left + 3, insetsImagem.bottom, insetsImagem.right));


        PinstripePainter pintorListraVertical = new PinstripePainter(
                new Color(155, 155, 155, 10), 0.);
        pintorListraVertical.setSpacing(2);
        pintorListraVertical.setStripeWidth(2);

        PinstripePainter pintorListraHorizontal = new PinstripePainter(
                new Color(155, 155, 155, 10), 90.);
        pintorListraHorizontal.setSpacing(2);
        pintorListraHorizontal.setStripeWidth(2);

        BufferedImage imagemDeFundo2 = null;
        try {
            imagemDeFundo2 = ImageIO.read(getClass().getResource(ConstantesDeCaminho.logoBier));
        } catch (IOException ex) {
            Logger.getLogger(PintorDeBackground.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        //ImagePainter pintorImagem2 = new ImagePainter(imagemDeFundo2);
        ImagePainter pintorImagem2 = (ImagePainter) this.pintorIcone(ConstantesDeCaminho.logoBier, 75, 75);
        pintorImagem2.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        pintorImagem2.setVerticalAlignment(VerticalAlignment.CENTER);
        pintorImagem2.setAntialiasing(true);

        CompoundPainter pintorComposto = new CompoundPainter(pintorSolido,pintorSolido2, pintorImagem2, pintorListraVertical, pintorListraHorizontal, pintorDeSombraTexto, pintorDeTexto);
        return (pintorComposto);
    }

    public Painter pintarPainelMenu(JComponent j) {
        PinstripePainter linhasVerticais = new PinstripePainter(
                new Color(155, 155, 155, 15), 0.);
        linhasVerticais.setSpacing(2);
        linhasVerticais.setStripeWidth(2);
        PinstripePainter linhasHorizontais = new PinstripePainter(
                new Color(155, 155, 155, 15), 90.);
        linhasHorizontais.setSpacing(2);
        linhasHorizontais.setStripeWidth(2);
        Color cor1 = new Color(20, 20, 20,100);
        Color cor2 = new Color(0, 0, 0,100);
        Point2D start = new Point2D.Float(0, 0);
        Point2D end = new Point2D.Float(0, (float) ((j.getPreferredSize().getHeight() - 1) / 2));
        float[] dist = {0.0f, 1.0f};
        Color[] colors = {cor1, cor2};
        LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors);
        MattePainter pintorSolido = new MattePainter(new Color(0, 0, 0,100));
        GlossPainter gp = new GlossPainter(new Color(20, 20, 20,100),
                GlossPainter.GlossPosition.TOP);

                Font fonteTrueType = null;
        try {
            fonteTrueType = Font.createFont(Font.TRUETYPE_FONT, getClass().getClassLoader().getResourceAsStream("biermanager/fontes/arcade.ttf"));
        } catch (FontFormatException ex) {
            Logger.getLogger(PainelTopo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PainelTopo.class.getName()).log(Level.SEVERE, null, ex);
        }
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fonteTrueType);

        String titulo = ConstantesDeJogo.menuInicial;
        TextPainter pintorDeTexto = new TextPainter(titulo, new Font(fonteTrueType.getFontName(), Font.PLAIN, 20));
        pintorDeTexto.setFillPaint(new Color(255, 255, 255,225));
        pintorDeTexto.setHorizontalAlignment(HorizontalAlignment.CENTER);
        pintorDeTexto.setVerticalAlignment(VerticalAlignment.TOP);



        CompoundPainter pintorComposto = new CompoundPainter(pintorSolido,gp,pintorDeTexto);
        pintorComposto.setAntialiasing(true);
        return (pintorComposto);
    }

    public Painter pintarPainelCaderno(JComponent j) {
        PinstripePainter linhasVerticais = new PinstripePainter(
                new Color(155, 155, 235, 80), 90.);
        linhasVerticais.setStripeWidth(2);
        linhasVerticais.setSpacing(30);
        Color cor1 = new Color(255, 255, 255,255);
        Color cor2 = new Color(255, 255, 255,155);
        Point2D start = new Point2D.Float(0, 0);
        Point2D end = new Point2D.Float(0, (float) (j.getPreferredSize().getHeight() - 1));
        float[] dist = {0.0f, 1.0f};
        Color[] colors = {cor1, cor2};
        LinearGradientPaint gradientePaint = new LinearGradientPaint(start, end, dist, colors);
        MattePainter pintorSolido = new MattePainter(gradientePaint);
        CompoundPainter pintorComposto = new CompoundPainter(pintorSolido, linhasVerticais);
        pintorComposto.setAntialiasing(true);
        return (pintorComposto);
    }

    public Painter pintorPainelImagem(JComponent j, String caminhoDaImagemDeFundo) {
        BufferedImage background = null;
        try {
            background = ImageIO.read(getClass().getResource(caminhoDaImagemDeFundo));
        } catch (IOException ex) {
            Logger.getLogger(JComponent.class.getName()).log(Level.SEVERE, null, ex);
        }
        Image imagem = background.getScaledInstance(j.getWidth(), j.getHeight(), Image.SCALE_SMOOTH);
        BufferedImage bimage = new BufferedImage(imagem.getWidth(null), imagem.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = bimage.createGraphics();
        g.drawImage(imagem, 0, 0, null);
        g.dispose();
        ImagePainter img = new ImagePainter(bimage);
        MattePainter mp = new MattePainter(new Color(0,0,0,150));
        return (new CompoundPainter(img,mp));
    }

    public Painter pintorIcone(String caminhoIcone,int largura, int altura) {
        BufferedImage background = null;
        try {
            background = ImageIO.read(getClass().getResource(caminhoIcone));
        } catch (IOException ex) {
            Logger.getLogger(JComponent.class.getName()).log(Level.SEVERE, null, ex);
        }
        Image imagem = background.getScaledInstance(largura, altura, Image.SCALE_SMOOTH);
        BufferedImage bimage = new BufferedImage(imagem.getWidth(null), imagem.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = bimage.createGraphics();
        g.drawImage(imagem, 0, 0, null);
        g.dispose();
        ImagePainter img = new ImagePainter(bimage);
        img.setVerticalAlignment(VerticalAlignment.TOP);
        return (img);
    }

    public Painter pintarCapsulaPreto(JComponent j) {
        CapsulePainter caps = new CapsulePainter(Portion.Full);
        caps.setFillPaint(new Color(0, 0, 0, 255));
        CompoundPainter cp = new CompoundPainter(caps);
        cp.setAntialiasing(true);
        return (cp);
    }
    public Painter pintarCinzaTransparente(JComponent j) {
        MattePainter mp = new MattePainter(new Color(0, 0, 0, 90));
        return (mp);
    }

    public Painter pintarDegradePreto(JComponent j) {
        Color cor1 = new Color(0, 0, 0, 255);
        Color cor2 = new Color(0,0,0,0);
        Point2D start = new Point2D.Float(0, 0);
        Point2D end = new Point2D.Float(0, (float) ((j.getPreferredSize().getHeight() - 1)));
        float[] dist = {0.0f, 1.0f};
        Color[] colors = {cor1, cor2};
        LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors, CycleMethod.NO_CYCLE, ColorSpaceType.SRGB, AffineTransform.getQuadrantRotateInstance(0));
        MattePainter mp = new MattePainter(p);
        return (mp);
    }

    public Painter pintarBotaoSemFocoCapsulaPreto() {
        CapsulePainter caps = new CapsulePainter(Portion.Full);
        caps.setFillPaint(new Color(180, 180, 180, 210));
        GlossPainter gp = new GlossPainter(new Color(110, 110, 110, 230),
                GlossPainter.GlossPosition.BOTTOM);
        CompoundPainter cp = new CompoundPainter(caps, gp);
        cp.setAntialiasing(true);
        return (cp);
    }

    public Painter pintarBotaoComFocoCapsulaPreto() {
        CapsulePainter caps = new CapsulePainter(Portion.Full);
        caps.setFillPaint(new Color(30, 155, 30, 200));
        GlossPainter gp = new GlossPainter(new Color(20, 60, 20, 230),
                GlossPainter.GlossPosition.BOTTOM);
        CompoundPainter cp = new CompoundPainter(caps, gp);
        cp.setAntialiasing(true);
        return (cp);
    }

    public Painter pintarPainelTransparente() {
        MattePainter mp = new MattePainter(new Color(255, 255, 255, 0));
        return (mp);
    }

    public Painter pintarPainelSelecionar() {
        int width = 10;
        int height = 10;
        Color color1 = new Color(255, 240, 240, 150);
        Color color2 = new Color(255, 255, 255, 0);
        LinearGradientPaint gradientPaint =
                new LinearGradientPaint(30.0f, 100.0f, width, height,
                new float[]{0.3f, 1.0f},
                new Color[]{color2, color1});
        MattePainter mattePainter = new MattePainter(gradientPaint);
        return (mattePainter);
    }
    public Painter pintarPainelSelecionado() {
        int width = 10;
        int height = 10;
        Color color1 = new Color(255, 255, 255, 80);
        Color color2 = new Color(255, 255, 255, 0);
        LinearGradientPaint gradientPaint =
                new LinearGradientPaint(0.0f, 0.0f, width, height,
                new float[]{0.3f, 1.0f},
                new Color[]{color1, color2});
        MattePainter mattePainter = new MattePainter(new Color(200,200,200,200));//gradientPaint);
        return (mattePainter);
    }
    public Painter pintarBarraTitulo(JComponent d ){
        Shape roundRectangle = new RoundRectangle2D.Float(0, 0,d.getSize().width,d.getSize().height, 30, 30);
        ShapePainter s = new ShapePainter(roundRectangle);
        Color cor1 = new Color(50, 50, 50,50);
        Color cor2 = new Color(0,0,0,50);
        Point2D start = new Point2D.Float(0, 0);
        Point2D end = new Point2D.Float((float) ((d.getSize().getWidth() - 1)),0);
        float[] dist = {0.0f, 0.5f,1.0f};
        Color[] colors = {cor1, cor2, cor1};
        LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REFLECT, ColorSpaceType.SRGB, AffineTransform.getQuadrantRotateInstance(0));
        s.setFillPaint(p);
        s.setInterpolation(Interpolation.Bicubic);
        return(s);
    }
        public Painter pintarCorpoPainel(JComponent d ){
        Shape roundRectangle = new RoundRectangle2D.Float(0, 0,d.getSize().width,d.getSize().height, 50, 50);
        ShapePainter s = new ShapePainter(roundRectangle);
        s.setFillPaint(new Color(35,35,35,35));
        return(s);
    }
    public Painter pintarTituloTabela(String titulo) {
        CapsulePainter caps = new CapsulePainter(Portion.Top);
        caps.setFillPaint(new Color(20, 60, 20, 230));

        Font fonteTrueType = null;
        try {
            fonteTrueType = Font.createFont(Font.TRUETYPE_FONT, getClass().getClassLoader().getResourceAsStream("biermanager/fontes/biermanager.ttf"));
        } catch (FontFormatException ex) {
            Logger.getLogger(PainelTopo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PainelTopo.class.getName()).log(Level.SEVERE, null, ex);
        }
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fonteTrueType);

        TextPainter pintorDeTexto = new TextPainter(titulo, new Font(fonteTrueType.getFontName(), Font.PLAIN, 14));
        pintorDeTexto.setInsets(new Insets(0,20,pintorDeTexto.getInsets().bottom,pintorDeTexto.getInsets().right));
        pintorDeTexto.setFillPaint(new Color(255, 255, 255,225));
        pintorDeTexto.setHorizontalAlignment(HorizontalAlignment.LEFT);
        pintorDeTexto.setVerticalAlignment(VerticalAlignment.CENTER);
        //pintorDeTexto.setAntialiasing(true);
        CompoundPainter cp = new CompoundPainter(caps,pintorDeTexto);
        //cp.setAntialiasing(true);
        return (cp);
    }
}
